package org.lzuploader.gui.helpers;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

/**
 * Class to easily display some message boxes.
 * @author Emmanuel Cron
 *
 */
public final class EasyMessageBox
{
    /** User should never instantiate this class. */
    private EasyMessageBox()
    {
        throw new UnsupportedOperationException();
    }
    
    /**
     * Displays an information dialog with a single "ok" button.
     * @param title     Title of the message box
     * @param contents  Contents of the message box
     */
    public static void info(final String title, final String contents)
    {
        displayDialog(title, contents, SWT.ICON_INFORMATION + SWT.OK);
    }
    
    /** Displays an error dialog with a single "ok" button.
     * @param title     Title of the message box
     * @param contents  Contents of the message box
     */
    public static void error(final String title, final String contents)
    {
        displayDialog(title, contents, SWT.ICON_ERROR + SWT.OK);
    }
    
    /**
     * Displays a Yes/No dialog.
     * @param title     Title of the message box
     * @param contents  Contents of the message box
     * @return int The ID of the button that was selected to dismiss the message box (e.g. SWT.OK, SWT.CANCEL, etc...)
     */
    public static int yesno(final String title, final String contents)
    {
        return displayDialog(title, contents, SWT.ICON_QUESTION + SWT.YES + SWT.NO);
    }
    
    /** Displays a dialog with the given style.
     * @param title     Title of the dialog
     * @param contents  Contents of the dialog
     * @param style     Style of the dialog
     * @return The ID of the button that was selected to dismiss the message box (e.g. SWT.OK, SWT.CANCEL, etc...)
     */
    private static int displayDialog(final String title, final String contents, final int style)
    {
        final Display display = Display.getCurrent();
        
        if (display != null)
        {
            Shell shell = null;
            for (int i = 0 ; i < display.getShells().length ; i++)
            {
                if (display.getShells()[i] != null)
                {
                    shell = display.getShells()[i];
                    break;
                }
            }
            
            final MessageBox messageBox = new MessageBox(
                shell,
                style
            );
            messageBox.setText(title);
            messageBox.setMessage(contents);
            return messageBox.open();
        }
        
        return SWT.NONE;
    }
}
